
package Test;

import java.io.File;

import junit.framework.TestCase;
import svgedit.gui.Frame;
import svgedit.svg.SVGCircleElement;
import svgedit.svg.SVGElement;

/**
 *  Test case when move an element and test when undo this execution
 *
 */
public class MoveElementTest extends TestCase {

	/**
	 *  Get Frame object
	 * */
	private static Frame frame;
	/**
	 *  Choose a circle element which is also under root group directly
	 * */
	private SVGCircleElement selectedElem;
	
	/**
	 *   Backup the selected element cx and cy
	 * */
	private float backupCX;
	private float backupCY;
	
	
	/**
	 *   Constructor for insertElement test
	 * */
	public MoveElementTest(String arg0) {
		super(arg0);
		frame = new Frame();
		File file = new File("src/testfile/test.svg");
        frame.openFile(file);
        selectedElem = getSelectedElement();
        backupCX = selectedElem.getCX().getValueInSpecifiedUnits();
        backupCY = selectedElem.getCY().getValueInSpecifiedUnits();
	}
	

	/**
	 *   Get a circle element in this file
	 *   
	 *   @return elem to get the selected item
	 * */
	public SVGCircleElement getSelectedElement(){
		for (SVGElement elem: frame.getDocument()){
			if (elem instanceof SVGCircleElement && elem.getParent() == frame.getDocument().getRootGroup()){
				return (SVGCircleElement)elem;
			}
		}
		return null;
	}
	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();	
	}
	
	/**
	 *  Test unit when move an element
	 * */
	public void testMoveElementTest(){
		MoveOnElement(selectedElem);
	}
	
	/**
	 *  Test unit when undo the move execution
	 * */
	public void testUndoMoveElementTest(){
		MoveBackElement(selectedElem);
	}
	
	/**
	 *  Move the elemet back to its original position
	 *  
	 *  @param element the moved element
	 * */
	public void MoveBackElement(SVGCircleElement element){
		float expectedX = backupCX;
		float expectedY = backupCY;
		element.getCX().setValueInSpecifiedUnits(backupCX);
		element.getCY().setValueInSpecifiedUnits(backupCY);
		float actualX = element.getCX().getValueInSpecifiedUnits();
	    float actualY = element.getCY().getValueInSpecifiedUnits();

	    assertEquals(expectedX,actualX);
		assertEquals(expectedY,actualY);
	}
	
	/**
	 *  Move the element to a certain position
	 *  
	 *  @param element the moved element
	 * */
	public void MoveOnElement(SVGCircleElement element){
		float expectedX = 300.0f;
		float expectedY = 200.0f;
		element.getCX().setValueInSpecifiedUnits(300.0f);
		element.getCY().setValueInSpecifiedUnits(200.0f);
		float actualX = element.getCX().getValueInSpecifiedUnits();
	    float actualY = element.getCY().getValueInSpecifiedUnits();
		
		assertEquals(expectedX,actualX);
		assertEquals(expectedY,actualY);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

}
